Ext.onReady(function(){
    Ext.QuickTips.init();
    Ext.BLANK_IMAGE_URL = 'ext3.2/resources/images/default/s.gif';
    var gridTitle = '电子杂志管理';
    var findall = "corpPublication_all.do";
    var getNewIdUrl = "corpPublication_newId.do";
    var addMsg = 'corpPublication_add.do';
    var deleteMsg = 'corpPublication_delete.do';
    var modifyMsg = 'corpPublication_modify.do';
    
    var renderTo = 'corpPublication_grid';
    var seqValue = "corpPublication_seq"; // 序列号名称
    var identityName = "id"; // 主键名称
    var pageSize = 18;
    var gridHeight = 500;
    
    var sm = new Ext.grid.CheckboxSelectionModel();
    var cm = new Ext.grid.ColumnModel([sm, {
        header: "编号",
        width: 100,
        dataIndex: 'id'
    }, {
        header: "类型",
        width: 100,
        sortable: true,
        dataIndex: 'typeId',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "企业",
        width: 100,
        dataIndex: 'corpId',
        editor: {
            xtype: 'textarea'
        }
    }, {
        header: "标题",
        width: 100,
        dataIndex: 'title',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "封面",
        width: 100,
        dataIndex: 'thumbPath',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "宽度",
        width: 100,
        dataIndex: 'width',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "高度",
        width: 100,
        dataIndex: 'height',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "背景声音",
        width: 100,
        dataIndex: 'backgroundSound',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "背景循环次数",
        width: 100,
        dataIndex: 'backgroundRepeat',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "Alpha",
        width: 100,
        dataIndex: 'backgroundAlpha',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "声音循环",
        width: 100,
        dataIndex: 'sounLoop',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "密码",
        width: 100,
        dataIndex: 'password',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "点击次数",
        width: 100,
        dataIndex: 'hits',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "备注",
        width: 100,
        dataIndex: 'comments',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "审核",
        width: 100,
        dataIndex: 'isAuditing',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "创建时间",
        width: 100,
        dataIndex: 'createDate',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "修改时间",
        width: 100,
        dataIndex: 'modDate',
        editor: {
            xtype: 'textfield'
            //xtype :'datefield'
        }
    }, {
        header: "路径",
        width: 100,
        dataIndex: 'endPath',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "背景",
        width: 100,
        dataIndex: 'background',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "广告宽度",
        width: 100,
        dataIndex: 'adWidth',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "广告高度",
        width: 100,
        dataIndex: 'adHeight',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "页数",
        width: 100,
        dataIndex: 'pagesCount',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "页数",
        width: 100,
        dataIndex: 'pubmemSize',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "是否有效",
        width: 100,
        dataIndex: 'isValid',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "是否推荐",
        width: 100,
        dataIndex: 'isCommend',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "路径",
        width: 100,
        dataIndex: 'pubPath',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "杂志类型",
        width: 100,
        dataIndex: 'pubType',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "杂志数",
        width: 100,
        dataIndex: 'pubMagazineCount',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "页数",
        width: 100,
        dataIndex: 'pubZoom',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "页数",
        width: 100,
        dataIndex: 'pubBackgroudSize',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "页数",
        width: 100,
        dataIndex: 'pubadSize',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "日期",
        width: 100,
        dataIndex: 'pwdLapseDate',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "开始页",
        width: 100,
        dataIndex: 'pwdStartPage',
        editor: {
            xtype: 'textfield'
        }
    }, {
        header: "是否公司",
        width: 100,
        dataIndex: 'isCommpany',
        editor: {
            xtype: 'textfield'
        }
    }]);
    
    var reader = new Ext.data.JsonReader({
        root: 'rows', // rows : 为服务器端传送过来的记录数值对象
        totalProperty: 'total' // results 为服务器端json传送过来的记录的行数, count(*)运算
    }, ['id', 'typeId', 'corpId', 'title', 'thumbPath', 'width', 'height', 'backgroundSound', 'backgroundRepeat', 'backgroundAlpha', 'sounLoop', 'password', 'hits', 'comments', 'isAuditing', 'createDate', 'modDate', 'endPath', 'background', 'adWidth', 'adHeight', 'pagesCount', 'pubmemSize', 'isValid', 'isCommend', 'pubPath', 'pubType', 'pubMagazineCount', 'pubZoom', 'pubBackgroudSize', 'pubadSize', 'pwdLapseDate', 'pwdStartPage', 'isCommpany']);
    
    
    
    
    
    
    var auditHandler = function(pass){
        var s = grid.getSelectionModel().getSelections();
        if (!s) 
            return false;
        Ext.Msg.show({
            title: '审核',
            buttons: Ext.MessageBox.YESNOCANCEL,
            msg: pass == "true" ? '确认通过审核？' : '确认不通过审核？',
            fn: function(btn){
                var ids = "";
                for (var i = 0; i < s.length; i++) { // 要删除的记录ID拼串
                    ids = ids + s[i].get(identityName) + ",";
                }
                var conn = new Ext.data.Connection();
                conn.request({
                    url: "corpPublication_audit.do",
                    params: {
                        auditIds: ids,
                        ispass: pass
                    },
                    success: function(resp, opt){
                        store.reload();
                    },
                    failure: function(resp, opt){
                        Ext.Msg.alert('错误', '审核失败！');
                    }
                });
            }
        });
    };
    
    
    // -------------------------下面无需改动-----------------------------------------------------------
    
    var store = new Ext.data.Store({
        url: findall,
        reader: reader
    });
    
    
    var grid = new Ext.grid.GridPanel({
        renderTo: renderTo,
        frame: true,
        title: gridTitle,
        autoScroll: true,
        width: "auto",
        height: gridHeight,
        store: store,
        sm: sm,
        cm: cm,
        tbar: [{
            ref: '../removeBtn',
            text: ' 删除 ',
            disabled: true,
            handler: function(e){
                delHandler(e)
            } // 处理删除函数
        }, {
            ref: '../auditBtn',
            text: ' 审核 ',
            disabled: true,
            handler: function(){
                auditHandler("true");
            }
        }, {
            ref: '../unAuditBtn',
            text: ' 不通过审核 ',
            disabled: true,
            handler: function(){
                auditHandler("false");
            }
        }],
        viewConfig: {
            columnsText: "显示/隐藏列",
            sortAscText: "正序排列",
            sortDescText: "倒序排列"
        },
        bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: store,
            displayInfo: true,
            afterPageText: ' 共 {0} 页',
            beforePageText: '页 ',
            firstText: '第一页 ',
            prevText: '前一页',
            nextText: '下一页',
            lastText: '最后一页',
            refreshText: '刷新',
            displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
            emptyMsg: "没有记录"
        })
    
    });
    store.load({
        params: {
            start: 0,
            limit: pageSize
        }
    });
    
    grid.getSelectionModel().on('selectionchange', function(sm){
        grid.removeBtn.setDisabled(sm.getCount() < 1);
        grid.auditBtn.setDisabled(sm.getCount() < 1);
        grid.unAuditBtn.setDisabled(sm.getCount() < 1);
    });
    
    // 删除的处理函数
    var delHandler = function(e){
        var s = grid.getSelectionModel().getSelections();
        if (!s) 
            return false;
        Ext.Msg.show({
            title: '删除',
            buttons: Ext.MessageBox.YESNOCANCEL,
            msg: '确认删除？',
            fn: function(btn){
				if(btn == "no" || btn == "cancel") return;
                var ids = "";
                for (var i = 0; i < s.length; i++) { // 要删除的记录ID拼串
                    ids = ids + s[i].get(identityName) + ",";
                }
                var conn = new Ext.data.Connection();
                conn.request({
                    url: deleteMsg,
                    params: {
                        delIds: ids
                    },
                    success: function(resp, opt){
                        for (var i = 0; i < s.length; i++) { // 把store中相应的记录删掉
                            grid.getStore().remove(s[i]);
                        }
                        store.reload();
                    },
                    failure: function(resp, opt){
                        Ext.Msg.alert('错误', '删除记录失败！');
                    }
                });
            }
        });
    }
});
